package com.lagou.oss.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.GetObjectRequest;
import com.lagou.oss.bean.OSSResult;
import com.lagou.oss.config.AliyunConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.UUID;

@Service
public class UploadService {

    @Autowired
    private AliyunConfig aliyunConfig;

    @Autowired
    private OSS ossClient;

    private static final String[] IMAGE_TYPE = new String[]{"jpg", "jpeg", "png"};

    public OSSResult upload(MultipartFile file) {

        OSSResult OSSResult = new OSSResult();

        // 检查文件类型
        boolean isLegal = false;
        for (String type : IMAGE_TYPE) {
            if (StringUtils.endsWithIgnoreCase(file.getOriginalFilename(), type)) {
                isLegal = true;
                break;
            }
        }

        if (!isLegal) {
            OSSResult.setStatus("文件类型错误");
            return OSSResult;
        }

        // 检查文件大小
        long fileSize = aliyunConfig.getFileSize() * 1024 * 1024;
        if (file.getSize() > fileSize) {
            OSSResult.setStatus("文件大小超过限制");
            return OSSResult;
        }

        String fileName = file.getOriginalFilename();
        String filePath = getFilePath(fileName);

        try {
            ossClient.putObject(aliyunConfig.getBucketName(), filePath, new ByteArrayInputStream(file.getBytes()));
        } catch (Exception e) {
            e.printStackTrace();
            OSSResult.setStatus("上传失败");
            return OSSResult;
        }

        OSSResult.setStatus("done");
        OSSResult.setName(aliyunConfig.getUrlPrefix() + filePath);
        return OSSResult;
    }

    public OSSResult download(String fileName, String downloadPath, String downloadFileName) {

        OSSResult OSSResult = new OSSResult();

        if (StringUtils.isEmpty(downloadFileName)) {
            downloadFileName = fileName;
        }

        try {
            ossClient.getObject(new GetObjectRequest(aliyunConfig.getBucketName(), fileName),
                    new File(downloadPath + "/" + downloadFileName));
        } catch (Exception e) {
            e.printStackTrace();
            OSSResult.setStatus("下载失败");
            return OSSResult;
        }
        OSSResult.setStatus("done");
        OSSResult.setName(aliyunConfig.getUrlPrefix() + fileName);
        return OSSResult;
    }

    public OSSResult delete(String fileName) {
        OSSResult OSSResult = new OSSResult();
        try {
            ossClient.deleteObject(aliyunConfig.getBucketName(), fileName);
        } catch (Exception e) {
            e.printStackTrace();
            OSSResult.setStatus("删除失败");
            return OSSResult;
        }

        OSSResult.setStatus("done");
        OSSResult.setName(aliyunConfig.getUrlPrefix() + fileName);
        return OSSResult;
    }

    private String getFilePath(String fileName) {
//        LocalDateTime nowDateTime = LocalDateTime.now();
//        return nowDateTime.getYear() + "/" + nowDateTime.getMonthValue() + "/" + nowDateTime.getDayOfMonth() + "/"
//                + UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
        return UUID.randomUUID().toString() + fileName.substring(fileName.lastIndexOf("."));
    }
}
